package account

import (
	"context"
	"errors"
	"github.com/starmotioncf/common/third_account"
	"github.com/starmotioncf/common/third_account/apple"
	"github.com/starmotioncf/common/third_account/facebook"
	"github.com/starmotioncf/common/third_account/google"
	"github.com/starmotioncf/common/third_account/snapchat"
)

var ErrUnsupported = errors.New("unsupported account")

type Config struct {
	Apple apple.Config
}

type ThirdAccount struct {
	accounts map[third_account.IdentityType]third_account.Auth
}

func NewAccountAuth(conf Config) *ThirdAccount {
	thirdAccounts := map[third_account.IdentityType]third_account.Auth{
		third_account.IdentityTypeApple:    apple.NewThirdAccountApple(conf.Apple),
		third_account.IdentityTypeGoogle:   third_account.AuthFunc(google.AuthToken),
		third_account.IdentityTypeSnapchat: third_account.AuthFunc(snapchat.SnapchatAuthToken),
		third_account.IdentityTypeFacebook: third_account.AuthFunc(facebook.FacebookAuthToken),
	}
	return &ThirdAccount{
		accounts: thirdAccounts,
	}
}

func (account *ThirdAccount) AuthToken(ctx context.Context, identType third_account.IdentityType, token string) (_ third_account.AuthPayload, err error) {
	thirdAccount, ok := account.accounts[identType]
	if !ok {
		return nil, ErrUnsupported
	}
	return thirdAccount.AuthToken(ctx, token)
}
